import request from '@/utils/request'

// 查询农贸市场列表
export function listMarket(query) {
  return request({
    url: '/yjnb/market/page',
    method: 'get',
    params: query
  })
}

// 查询农贸市场详细
export function getMarket(id) {
  return request({
    url: '/yjnb/market/getById/' + id,
    method: 'get'
  })
}

// 新增农贸市场
export function addMarket(data) {
  return request({
    url: '/yjnb/market/add',
    method: 'post',
    data: data
  })
}

// 修改农贸市场
export function updateMarket(data) {
  return request({
    url: '/yjnb/market/update',
    method: 'put',
    data: data
  })
}

// 批量删除农贸市场
export function delMarket(ids) {
  // 确保传入的是数组
  const idList = Array.isArray(ids) ? ids : [ids];

  // 过滤掉空值
  const validIds = idList.filter(id => id !== null && id !== undefined);

  if (validIds.length === 0) {
    return Promise.reject(new Error('请选择要删除的市场'));
  }

  // 拼接URL参数：ids=1&ids=2&ids=3
  const queryString = validIds.map(id => `ids=${id}`).join('&');

  const url = `/yjnb/market/deletes?${queryString}`;

  return request({
    url: url,
    method: 'delete'
  })
}

// 导出农贸市场数据
export function exportMarket() {
  return request({
    url: '/common/excel/market/export',
    method: 'get',
    responseType: 'blob'
  })
}
